<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"/>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>

    <div th:insert="publicHeader.html"></div>
    <script th:src="@{/publicHeader.js}"></script>


    <div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px">

        <div class="container">

            <div class="row">
                <a href="/login.html">已有账号?立即去登录</a>
                <div class="col-md-4 col-md-offset-4" style="border: 1px solid #cccccc ;border-radius: 5px;margin-top: 10px">
                    <form>
                        <div class="form-group">
                            <label for="username">用户名:</label>
                            <input type="text" v-model="registerInfo.username" name="username" class="form-control" id="username" placeholder="请输入用户名">
                        </div>
                        <div class="form-group">
                            <label for="password">密码</label>
                            <input type="password" v-model="registerInfo.password" name="password" class="form-control" id="password" placeholder="请输入密码">
                        </div>
                        <div class="form-group">
                            <label for="repass">确认密码:</label>
                            <input type="password" v-model="registerInfo.repass" name="repass" class="form-control" id="repass" placeholder="请输入确认密码">
                        </div>

                        <div class="form-group">
                            <label for="email">邮箱:</label>
                            <input type="email" v-model="registerInfo.email" name="email" class="form-control" id="email" placeholder="请输入邮箱地址">
                            <button class="btn btn-primary" id="getEmailCodeBtn" @click.prevent="getEmailCode">获取验证码</button>
                            <p style="color: red" v-text="errorInfo"></p>
                        </div>
                        <div class="form-group">
                            <label for="emailCode">请输入验证码:</label>
                            <input type="text" v-model="registerInfo.emailCode" name="emailCode" class="form-control" id="emailCode" placeholder="请输入邮箱验证码">
                        </div>

                        <button class="btn btn-success" @click.prevent="toRegister()" >注册</button>
                    </form>
                </div>
            </div>

        </div>

    </div>


<script>

    console.log("------register-----")

    let app = new Vue({
        el:"#app",
        data:{
            errorInfo:"",
            registerInfo:{
                username:"",
                password:"",
                repass:"",
                email:"",
                emailCode:""
            }

        },
        methods:{
            getEmailCode(){
                console.log(this.registerInfo.email)
                /* js中空字符串相当于false */
                if (!this.registerInfo.email){
                    alert("邮箱地址不能为空");
                    return;
                }
                let vueObj = this;
                $.post("/user/getEmailCode",{email:vueObj.registerInfo.email},function (data) {
                    if (data == "ok"){
                        let btn = $("#getEmailCodeBtn");
                        let time = 60;
                        btn.attr("disabled",true)  //置为不可用
                        btn.text(time+"秒可重新获取验证码");

                        let timeInterval = setInterval(function (){
                            time--;
                            if (time <= 0){
                                clearInterval(timeInterval)
                                btn.attr("disabled",false)
                                btn.text("重新获取验证码");
                            }else {
                                btn.text(time+"秒可重新获取验证码");
                            }
                        },1000)     //此方法每1000毫秒执行一次,即1秒执行一次
                        vueObj.errorInfo = "";
                    }else {
                        vueObj.errorInfo = data;
                    }
                })
            },
            toRegister(){
                console.log(this.registerInfo);
                let vueObj = this;

                $.post("/user/register",this.registerInfo,function (data) {
                    if (data == "ok"){
                        alert("注册成功!")
                    }else {
                        alert(data);
                    }

                })
            }
        },
        created(){

        }
    })

</script>


<div th:insert="publicFooter.html"></div>


</body>
</html>